import os

import pandas as pd

from topsis_zbj.topsis_config import topsis_config

DATAPATH = topsis_config.Data_Dir


def read_Pinfo(fname='zbj_21514668.xls', path=DATAPATH) -> dict:
    p = {}
    ffullpath = os.path.join(DATAPATH, fname)
    pro_data1 = pd.read_excel(ffullpath, sheet_name='project_sheet')
    # pro_data2 = pd.read_excel(ffullpath, sheet_name='bids_sheet')
    dicpro = pro_data1.to_dict()
    for k in dicpro:
        dicpro[k] = dicpro[k][0]
    return dicpro


def read_bids(fname='pinfo_99994.xls', path=DATAPATH) -> dict:
    bids = pd.read_excel(path + fname, sheet_name='bids_sheet')
    return bids


def get_imlist():
    return [os.path.join(f) for f in os.listdir(DATAPATH) if f.endswith('.xls')]


def get_Pinfo_Data_Search(timeFrom='2015'):
    pinfo_namelist = get_imlist()
    plist = []
    states = []
    for pname in pinfo_namelist:
        p = {}
        p = read_Pinfo(pname)
        if p['ctime'] > timeFrom:
            plist.append(p)
    return plist


if __name__ == '__main__':
    # infolist = get_Pinfo_Data_Search()
    # datas = pd.DataFrame(infolist)
    #     datas.to_excel('pinfo_sum.xls')
    #     # csv = pd.read_excel(topsis_config.Data_Dir + 'test.xlsx', sheet_name='Sheet2')
    pro_data1, pro_data2 = read_Pinfo()
    # d_fin = datas[datas['contractor_name'] != ""]
    # d_fin_2017 = d_fin[d_fin['date'] > '2017']
    # d_fin_2017_ex = d_fin_2017[d_fin_2017['win'] != "被冻结"]
    # d_fin_2017_ex.to_excel('d_fin_2017_ex.xls')
